#include "BLDC_change.h"
uint32_t modulation_value = 0;
char adc_check_flag = 0, speed_check_flag = 0;
pole_status pole_status_set;
u16 i_adc_max_average = 0, i_adc_min_average = 0;
uint32_t rise_down_time = 0, first_rise_time = 0, rise_time_max = 0, first_down_time = 0, down_time_max = 0, rise_down_time_count = 0, rise_down_low_pass_filter = 0;
uint32_t temp_u_rise_time_to_see = 0, temp_u_down_time_to_see = 0, temp_v_rise_time_to_see = 0, temp_v_down_time_to_see = 0, temp_w_rise_time_to_see = 0, temp_w_down_time_to_see = 0, temp_count_every = 0;
uint32_t adc1_u_high_value = 0, adc1_u_high_value_sum = 0, adc1_v_high_value = 0, adc1_v_high_value_sum = 0, adc1_w_high_value = 0, adc1_w_high_value_sum = 0;
uint32_t adc1_u_Low_value = 0, adc1_u_Low_value_sum = 0, adc1_v_Low_value = 0, adc1_v_Low_value_sum = 0, adc1_w_Low_value = 0, adc1_w_Low_value_sum = 0;
uint32_t speed = 0, speed_real = 0, counter = 0, protect_counter = 0, speed_return = 0, comper_number = 0;

extern uint16_t u16ADC1[10];
extern uint32_t u16ADC1_16in1_adc[10];
extern uint32_t u16ADC1_2in1_adc[10];

//换向设置
void motor_reserving_jugement(void)
{
    if (adc1_u_high_value && adc1_v_high_value && adc1_w_high_value)
    {
        /* code */
        if (rise_down_time > 0xcc || (rise_down_time > 8 * rise_down_low_pass_filter && rise_down_low_pass_filter > 15))
        {

            check_bemf_u_zero_check_reset();
            check_bemf_v_zero_check_reset();
            check_bemf_w_zero_check_reset();
            adc_flag_set_and_check_status();
        }

        if (pole_status_set.u_pole_status == 2)
        {
            check_bemf_u_zero_down_cross(u16ADC1_2in1_adc[0] * 8);
        }
        else if (pole_status_set.u_pole_status == 5)
        {
            check_bemf_u_zero_rise_cross(u16ADC1_2in1_adc[0] * 8);
        }
        else if (pole_status_set.v_pole_status == 2)
        {
            check_bemf_v_zero_down_cross(u16ADC1_2in1_adc[1] * 8);
        }
        else if (pole_status_set.v_pole_status == 5)
        {
            check_bemf_v_zero_rise_cross(u16ADC1_2in1_adc[1] * 8);
        }
        else if (pole_status_set.w_pole_status == 2)
        {
            check_bemf_w_zero_down_cross(u16ADC1_2in1_adc[2] * 8);
        }
        else if (pole_status_set.w_pole_status == 5)
        {
            check_bemf_w_zero_rise_cross(u16ADC1_2in1_adc[2] * 8);
        }
    }
    else
    {
        adc_flag_set_and_check_status();
    }
}
// if (rise_down_time_count == 20)
// {
//     rise_down_time_count = 21;
// }
void low_pass_of_change_time()
{
    rise_down_low_pass_filter = rise_down_low_pass_filter / 2 + rise_down_time / 2;
    rise_down_time = rise_down_low_pass_filter;
}
void check_bemf_u_zero_rise_cross(uint32_t check_value)
{
    rise_down_time_count++;

    if (first_rise_time == 0)
    {
        rise_down_time++;
        if (check_value > ((adc1_u_high_value + adc1_u_Low_value) / 2) + 40 && rise_down_time > 13)
        {
            if (rise_time_max < rise_down_time)
            {
                rise_time_max = rise_down_time;
            }
            first_rise_time = rise_down_time;
            low_pass_of_change_time();
        }
    }
    else
    {
        if (--rise_down_time <= 0)
        {
            adc_flag_set_and_check_status();
            check_bemf_u_zero_check_reset();
        }
    }
}
void check_bemf_v_zero_rise_cross(uint32_t check_value)
{
    rise_down_time_count++;

    if (first_rise_time == 0)
    {
        rise_down_time++;
        if (check_value > ((adc1_v_high_value + adc1_v_Low_value) / 2) + 40 && rise_down_time > 13)
        {
            if (rise_time_max < rise_down_time)
            {
                rise_time_max = rise_down_time;
            }
            first_rise_time = rise_down_time;
            low_pass_of_change_time();
        }
    }
    else
    {
        if (--rise_down_time <= 0)
        {
            adc_flag_set_and_check_status();
            check_bemf_v_zero_check_reset();
        }
    }
}
void check_bemf_w_zero_rise_cross(uint32_t check_value)
{
    rise_down_time_count++;
    if (first_rise_time == 0)
    {
        rise_down_time++;
        if (check_value > ((adc1_w_high_value + adc1_w_Low_value) / 2) + 40 && rise_down_time > 13)
        {
            if (rise_time_max < rise_down_time)
            {
                rise_time_max = rise_down_time;
            }
            first_rise_time = rise_down_time;
            low_pass_of_change_time();
        }
    }
    else
    {
        if (--rise_down_time <= 0)
        {
            adc_flag_set_and_check_status();
            check_bemf_w_zero_check_reset();
        }
    }
}
void check_bemf_u_zero_down_cross(uint32_t check_value)
{
    rise_down_time_count++;
    if (first_down_time == 0)
    {
        rise_down_time++;
        if (check_value < ((adc1_u_high_value + adc1_u_Low_value) / 2) && rise_down_time > 13)
        {
            if (down_time_max < rise_down_time)
            {
                down_time_max = rise_down_time;
            }
            first_down_time = rise_down_time;
            low_pass_of_change_time();
        }
    }
    else
    {
        if (--rise_down_time <= 0)
        {
            adc_flag_set_and_check_status();
            check_bemf_u_zero_check_reset();
        }
    }
}
void check_bemf_v_zero_down_cross(uint32_t check_value)
{
    rise_down_time_count++;
    if (first_down_time == 0)
    {
        rise_down_time++;
        if (check_value < ((adc1_v_high_value + adc1_v_Low_value) / 2) && rise_down_time > 13)
        {
            if (down_time_max < rise_down_time)
            {
                down_time_max = rise_down_time;
            }
            first_down_time = rise_down_time;
            low_pass_of_change_time();
        }
    }
    else
    {
        if (--rise_down_time <= 0)
        {
            adc_flag_set_and_check_status();
            check_bemf_v_zero_check_reset();
        }
    }
}
void check_bemf_w_zero_down_cross(uint32_t check_value)
{
    rise_down_time_count++;
    if (first_down_time == 0)
    {
        rise_down_time++;
        if (check_value < ((adc1_w_high_value + adc1_w_Low_value) / 2) && rise_down_time > 13)
        {
            if (down_time_max < rise_down_time)
            {
                down_time_max = rise_down_time;
            }
            first_down_time = rise_down_time;
            low_pass_of_change_time();
        }
    }
    else
    {
        if (--rise_down_time <= 0)
        {
            adc_flag_set_and_check_status();
            check_bemf_w_zero_check_reset();
        }
    }
}
void check_bemf_u_zero_check_reset(void)
{
    if (rise_down_time != 0)
    {
        if (pole_status_set.u_pole_status == 3)
        {
            first_rise_time = rise_down_time;
        }
        if (pole_status_set.u_pole_status == 0)
        {
            first_down_time = rise_down_time;
        }
        rise_down_time = 0;
    }

    if (rise_down_time_count != 0)
    {
        temp_count_every = rise_down_time_count;
        rise_down_time_count = 0;
    }

    if (first_rise_time != 0)
    {
        temp_u_rise_time_to_see = first_rise_time;
        first_rise_time = 0;
    }
    if (first_down_time != 0)
    {
        temp_u_down_time_to_see = first_down_time;
        first_down_time = 0;
    }
}
void check_bemf_v_zero_check_reset(void)
{
    if (rise_down_time != 0)
    {
        if (pole_status_set.u_pole_status == 3)
        {
            first_rise_time = rise_down_time;
        }
        if (pole_status_set.u_pole_status == 0)
        {
            first_down_time = rise_down_time;
        }
        rise_down_time = 0;
    }

    if (rise_down_time_count != 0)
    {
        temp_count_every = rise_down_time_count;
        rise_down_time_count = 0;
    }

    if (first_rise_time != 0)
    {
        temp_v_rise_time_to_see = first_rise_time;
        first_rise_time = 0;
    }
    if (first_down_time != 0)
    {
        temp_v_down_time_to_see = first_down_time;
        first_down_time = 0;
    }
}
void check_bemf_w_zero_check_reset(void)
{
    if (rise_down_time != 0)
    {
        if (pole_status_set.u_pole_status == 3)
        {
            first_rise_time = rise_down_time;
        }
        if (pole_status_set.u_pole_status == 0)
        {
            first_down_time = rise_down_time;
        }
        rise_down_time = 0;
    }

    if (rise_down_time_count != 0)
    {
        temp_count_every = rise_down_time_count;
        rise_down_time_count = 0;
    }

    if (first_rise_time != 0)
    {
        temp_w_rise_time_to_see = first_rise_time;
        first_rise_time = 0;
    }
    if (first_down_time != 0)
    {
        temp_w_down_time_to_see = first_down_time;
        first_down_time = 0;
    }
}
//设置最大值
void set_u_max_value(u32 value)
{
    if (i_adc_max_average++ < 8)
    {
        adc1_u_high_value_sum += value;
    }
    else if (i_adc_max_average == 9)
    {
        adc1_u_high_value = adc1_u_high_value_sum / 8;
        adc1_u_high_value_sum = 0;
    }
    else
    {
        i_adc_max_average = 20;
    }
}
void set_v_max_value(u32 value)
{
    if (i_adc_max_average++ < 8)
    {
        adc1_v_high_value_sum += value;
    }
    else if (i_adc_max_average == 9)
    {
        adc1_v_high_value = adc1_v_high_value_sum / 8;
        adc1_v_high_value_sum = 0;
    }
    else
    {
        i_adc_max_average = 20;
    }
}
void set_w_max_value(u32 value)
{

    if (i_adc_max_average++ < 8)
    {
        adc1_w_high_value_sum += value;
    }
    else if (i_adc_max_average == 9)
    {
        adc1_w_high_value = adc1_w_high_value_sum / 8;
        adc1_w_high_value_sum = 0;
    }
    else
    {
        i_adc_max_average = 20;
    }
}
void set_u_min_value(u32 value)
{
    if (i_adc_min_average++ < 8)
    {
        adc1_u_Low_value_sum += value;
    }
    else if (i_adc_min_average == 9)
    {
        adc1_u_Low_value = adc1_u_Low_value_sum / 8;
        adc1_u_Low_value_sum = 0;
    }
    else
    {
        i_adc_min_average = 20;
    }
}
void set_v_min_value(u32 value)
{
    if (i_adc_min_average++ < 8)
    {
        adc1_v_Low_value_sum += value;
    }
    else if (i_adc_min_average == 9)
    {
        adc1_v_Low_value = adc1_v_Low_value_sum / 8;
        adc1_v_Low_value_sum = 0;
    }
    else
    {
        i_adc_min_average = 20;
    }
}
void set_w_min_value(u32 value)
{
    if (i_adc_min_average++ < 8)
    {
        adc1_w_Low_value_sum += value;
    }
    else if (i_adc_min_average == 9)
    {
        adc1_w_Low_value = adc1_w_Low_value_sum / 8;
        adc1_w_Low_value_sum = 0;
    }
    else
    {
        i_adc_min_average = 20;
    }
}
void clear_max_flag(void)
{
    i_adc_max_average = 0;
}
void clear_min_flag(void)
{
    i_adc_min_average = 0;
}
//电机旋转控制
void BLDC_motor_modulation_value_set(uint32_t modulation)
{
    modulation_value = modulation;
}
void motor_status_set(char u_status, char v_status, char w_status)
{
    pole_status_set.u_pole_status = u_status;
    pole_status_set.v_pole_status = v_status;
    pole_status_set.w_pole_status = w_status;
}
void motor_status_change_auto(void)
{
    pole_status_set.u_pole_status++;
    pole_status_set.v_pole_status++;
    pole_status_set.w_pole_status++;
    if (pole_status_set.u_pole_status >= 6)
    {
        pole_status_set.u_pole_status = 0;
    }
    if (pole_status_set.v_pole_status >= 6)
    {
        pole_status_set.v_pole_status = 0;
    }
    if (pole_status_set.w_pole_status >= 6)
    {
        pole_status_set.w_pole_status = 0;
    }
}
uint32_t get_compare_now(char pole_status)
{
    //    uint32_t return_number = 0;
    switch (pole_status)
    {
    case 0:
    case 1:
        return PWM_PERIOD - PWM_PERIOD / 1000 * modulation_value;

    case 2:
    case 5:
        return PWM_PERIOD - PWM_PERIOD / 1000 * modulation_value / 2;
    case 3:
    case 4:
        return PWM_PERIOD - 1;
    }
    return PWM_PERIOD - 1;
}
void BLDC_change_mode_compare(void)
{
    TIM_SetCompare1(TIM1, get_compare_now(pole_status_set.u_pole_status));
    TIM_SetCompare2(TIM1, get_compare_now(pole_status_set.v_pole_status));
    TIM_SetCompare3(TIM1, get_compare_now(pole_status_set.w_pole_status));
}
void motor_status_change_check_and_set()
{
    if (adc_check_flag && speed_check_flag)
    {
        adc_check_flag = 0;
        speed_check_flag = 0;
        protect_counter = 0;
        motor_status_change_auto();
        BLDC_change_mode_compare();
        speed_real++; //
    }
}
void adc_flag_set_and_check_status(void)
{
    adc_check_flag = 1;
    motor_status_change_check_and_set();
}
void speed_flag_set_and_check_status(void)
{
    speed_check_flag = 1;
    motor_status_change_check_and_set();
}
void set_speed(uint32_t speed_in)
{

    speed = speed_in;
    if (speed != 0)
    {
        comper_number = (10000 / 12 / speed);
    }
}
void speed_control_watching(void)
{
    if (speed != 0)
    {
        if (counter++ > comper_number)
        {
            counter = 0;
            speed_flag_set_and_check_status();
        }
        // if (protect_counter++ > 500)
        // {
        //     protect_counter = 0;
        //     // speed_flag_set_and_check_status();
        //     adc_flag_set_and_check_status();
        // }
    }
}
uint32_t get_speed_setted(void)
{
    return speed;
}
uint32_t get_speed_now(void)
{
    speed_return = speed_real;
    speed_real = 0;
    return speed_return;
}
